/* 
 * Copyright (C)2008 Vladimir Hozjan vladimir.hozjan@gmail.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */

/**
 * @file  tetrisBackground.cpp
 *
 * Implementation of tetris background
 *
 * @author Vladimir Hozjan
 * @date   30.12.2008 
 *
 * @remarks VHOZ [30.12.2008]: file created
 */
#include "tetrisBackground.h"
#include "tetrisElement.h"


/**
 * TetrisBackground
 *
 * Constructor creates background with specific size.
 *
 * @param[in] aWidth - width of background
 * @param[in] aHeight - height of background
 */
TetrisBackground::TetrisBackground( int aWidth, int aHeight ):
TetrisMatrix(aHeight,aWidth)
{

}

/**
 * ~TetrisBackground
 *
 * Destructor
 */
TetrisBackground::~TetrisBackground() {
}

/**
 * insertElement
 *
 * Insert element in the background.
 *
 * @param[in] aRow - row position for insertion
 * @param[in] aCol - column position for insertion
 * @param[in] aElement - element for insertion
 */
void TetrisBackground::insertElement( int aRow, int aCol, TetrisElement *aElement ) {
  unsigned short i,j;
  unsigned short size = aElement->getDimension();
  for(i = 0; i < size; i++ ) {
    for( j = 0; j < size; j++ ) {
      if ( aElement->getCell(i,j) != 0 ) {
        mMatrix[aRow + i][aCol + j] = aElement->getCell(i,j);
      }
    }
  }
}

/**
 * isSpaceForElement
 *
 * Method checks if is space for element at given position.
 *
 * @param[in] aRow - row position of element
 * @param[in] aCol - column position of element
 * @param[in] aElement - element
 *
 * @return True if is space in background, otherwise false.
 */
bool TetrisBackground::isSpaceForElement( int aRow, int aCol, TetrisElement *aElement ) const {
  bool isSpace = true;
  unsigned short i,j;
  if (aElement != 0) {
    unsigned short size = aElement->getDimension();
    /* for each cell in element */
    for( i = 0; i < size; i++) {
      for(j = 0; j < size; j++) {
        /*if is full cell*/
        if (aElement->getCell(i,j) != 0) {
          /*check if this cell fits into background*/
          if ( 
              (aRow + i >= 0) && 
              (aRow + i < mNumRows) && 
              (aCol + j >= 0) && 
              (aCol + j < mNumCols)
             )
          {
            /*check if is cell empty in background*/
            if ( mMatrix[aRow + i][aCol + j] != 0)  {
              isSpace = false;
            }
          } else {
            isSpace = false;
          }

          if (aRow + i < 0) {
          }

        }
      }
    }
  }
  return isSpace;
}

/**
 * countFullLines
 *
 * Count full lines in the background
 *
 * @return Number of full lines in the table
 */
unsigned short TetrisBackground::countFullLines() const {
  unsigned short retval = 0;
  unsigned short i,j;
  /* Go trough each row. */
  for (i = 0; i < mNumRows; i++) {
    unsigned short countCells = 0;
    /* Count full cells in row. */
    for ( j = 0 ; j < mNumCols ; j++ ){
      if( mMatrix[i][j] != 0 ) { 
        countCells++;
      }
    }
    /* Detect full line and increase counter. */
    if (countCells == mNumCols) {
      retval ++;
    }
  }
  return retval;
}

/**
 * clearFullLines
 *
 * Clear full lines from table
 */
void TetrisBackground::clearFullLines() {
  unsigned short line,j;
  /* For each line */
  for (line = 0; line < mNumRows; line++) {
    /* Count full cells */
    unsigned short countCells = 0;
    for ( j = 0 ; j < mNumCols ; j++ ){
      if( mMatrix[line][j] != 0 ) { 
        countCells++;
      }
    }

    /* Detect full line */
    if (countCells == mNumCols) {
      unsigned short i;
      /* Clear full line */
      for(i = line; i < mNumRows - 1; i++){
        for ( j = 0 ; j < mNumCols ; j++ ) {
          mMatrix[i][j]=mMatrix[i+1][j];
        }
      }

      /* Full line is erased, next line moved down - line must be decreased*/
      line--;
    }
  }
}
